from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
User = get_user_model()

class CaptchaModel(models.Model):
    """验证码模型"""
    email = models.EmailField(unique=True)
    captcha = models.CharField(max_length=4)
    create_time = models.DateTimeField(auto_now_add=True)


class BlogCategory(models.Model):  # 博客分类
    name = models.CharField(max_length=200,verbose_name="分类名称")
    def __str__(self):
        return self.name
    class Meta:
    # apple, apples
        verbose_name = '博客分类'
        verbose_name_plural = verbose_name

class Blog(models.Model):
    """博客内容"""
    title = models.CharField(max_length=200,verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    pub_time = models.DateTimeField(auto_now_add=True,verbose_name='发布时间')
    category = models.ForeignKey(BlogCategory, on_delete=models.CASCADE,verbose_name='分类')
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blogs',verbose_name='博客作者')
    def __str__(self):
        # 返回博客标题
        return self.title
    class Meta:
        verbose_name = '博客'
        verbose_name_plural = verbose_name
        ordering = ['-pub_time']

# 博客评论存储
class BlogComment(models.Model):
    """博客评论"""
    content = models.TextField(verbose_name='内容')
    pub_time = models.DateTimeField(auto_now_add=True,verbose_name='发布时间')
    blog = models.ForeignKey(Blog,on_delete=models.CASCADE,related_name='comments',verbose_name='所属博客')
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='comments',verbose_name='评论作者')
    def __str__(self):
        return self.content
    class Meta:
        verbose_name = '博客评论'
        verbose_name_plural = verbose_name
        ordering = ['-pub_time']

# 博客我的信息存储
# class BlogMyself(models.Model):

# models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _

class UserProfile(models.Model):
    """用户扩展资料"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    avatar = models.ImageField(
        _('头像'),
        upload_to='avatars/',
        default='avatars/default.png'
    )
    bio = models.TextField(_('个人简介'), max_length=500, blank=True)
    github = models.URLField(_('GitHub主页'), blank=True)
    linkedin = models.URLField(_('LinkedIn主页'), blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = _('用户资料')
        verbose_name_plural = _('用户资料')

    def __str__(self):
        return f"{self.user.username} 的资料"

class Message(models.Model):
    """消息中心模型"""
    class MessageType(models.TextChoices):
        SYSTEM = 'system', _('系统通知')
        COMMENT = 'comment', _('新评论')
        FAN = 'fan', _('新粉丝')
        LIKE = 'like', _('点赞通知')
        OTHER = 'other', _('其他消息')

    class MessageStatus(models.TextChoices):
        UNREAD = 'unread', _('未读')
        READ = 'read', _('已读')
        ARCHIVED = 'archived', _('已归档')

    recipient = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='messages',
        verbose_name=_('接收者')
    )
    title = models.CharField(_('标题'), max_length=100)
    content = models.TextField(_('内容'))
    msg_type = models.CharField(
        _('消息类型'),
        max_length=20,
        choices=MessageType.choices,
        default=MessageType.SYSTEM
    )
    status = models.CharField(
        _('状态'),
        max_length=20,
        choices=MessageStatus.choices,
        default=MessageStatus.UNREAD
    )
    related_object_id = models.PositiveIntegerField(_('关联对象ID'), null=True, blank=True)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)

    class Meta:
        verbose_name = _('消息')
        verbose_name_plural = _('消息')
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['recipient', 'status']),
        ]

    def __str__(self):
        return f"{self.get_msg_type_display()} - {self.title}"

class UserActivity(models.Model):
    """用户活动时间线"""
    class ActivityType(models.TextChoices):
        BLOG_PUBLISH = 'blog_publish', _('发布博客')
        COMMENT_POST = 'comment_post', _('发表评论')
        PROFILE_UPDATE = 'profile_update', _('更新资料')
        LOGIN = 'login', _('登录系统')
        FOLLOW = 'follow', _('关注用户')

    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='activities',
        verbose_name=_('用户')
    )
    activity_type = models.CharField(
        _('活动类型'),
        max_length=20,
        choices=ActivityType.choices
    )
    content = models.CharField(_('活动内容'), max_length=255)
    metadata = models.JSONField(_('元数据'), default=dict, blank=True)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('用户活动')
        verbose_name_plural = _('用户活动')
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user', 'activity_type']),
        ]

    def __str__(self):
        return f"{self.user} - {self.get_activity_type_display()}"

class Follow(models.Model):
    """用户关注关系"""
    follower = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='following',
        verbose_name=_('关注者')
    )
    followed = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='followers',
        verbose_name=_('被关注者')
    )
    created_at = models.DateTimeField(_('关注时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('关注关系')
        verbose_name_plural = _('关注关系')
        unique_together = ('follower', 'followed')
        indexes = [
            models.Index(fields=['follower', 'followed']),
        ]

    def __str__(self):
        return f"{self.follower} → {self.followed}"